package MainProgram;

import java.awt.Dimension;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RefineryUtilities;

/**
 * Represents the plot object.
 * @author Zbyszko
 */
public class Plot  extends JFrame {
    private static final long serialVersionUID = 1L;

    /**
     * Create the plot object responsible for presenting the plot of function.
     * @param title The string-name of the window which presents the plot.
     * @param seriesName The string-name of the data series.
     * @param trainingSet The set of training data.
     * @param desiredOutput The set of desired output data.
     * @param obtainedResult The set of obtained result data.
     */
    public Plot(String title, String seriesName, 
            double[][] trainingSet, double[][] desiredOutput, 
            double[][] obtainedResult) {
        
        super(title);
        final XYSeriesCollection dataset = createDataset(trainingSet, 
                desiredOutput, obtainedResult);
        
        final JFreeChart chart = ChartFactory.createXYLineChart(
            seriesName,
            "Training set", 
            "Desired output/Obtained result", 
            dataset,
            PlotOrientation.VERTICAL,
            false,
            true,
            false
        );
        
        XYPlot plot = (XYPlot) chart.getPlot();
        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        renderer.setSeriesLinesVisible(0, true);
        renderer.setSeriesShapesVisible(0, false);
        renderer.setSeriesLinesVisible(1, false);
        renderer.setSeriesShapesVisible(1, true); 
        plot.setRenderer(renderer);
        
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(1000, 570));
        setContentPane(chartPanel);
        this.pack();
        RefineryUtilities.centerFrameOnScreen(this);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
    
    /**
     * This method prepares a dataset from data, that suitable for a Chart.
     * @param trainingSet The set of training data.
     * @param desiredOutput The set of desired output data.
     * @param obtainedResult The set of obtained result data.
     * @return The dataset object which will be presented on the plot.
     */
    private XYSeriesCollection createDataset(double[][] trainingSet, 
            double[][] desiredOutput, double[][] obtainedResult) { 
        
        // create the dataset...
        final XYSeries series1 = new XYSeries("Desired output");
        final XYSeries series2 = new XYSeries("Obtained result");
        for (int i = 0; i < trainingSet.length; i++) {
            for (int j = 0; j < trainingSet[i].length; j++) {
                series1.add(trainingSet[i][j], desiredOutput[i][j]);
                series2.add(trainingSet[i][j], obtainedResult[i][j]);
            }
        }
        
        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series1);
        dataset.addSeries(series2);
        return dataset;              
    }
    
    public Plot(String title, String seriesName, 
            double[][] trainingSet, double[][] desiredOutput, 
            double[][] newTrainingSet, double[][] obtainedResult) {
        
        super(title);
        
        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(createSerie(trainingSet, desiredOutput, "Desired"));
        dataset.addSeries(createSerie(newTrainingSet, obtainedResult, "Obtained"));
        
        final JFreeChart chart = ChartFactory.createXYLineChart(
            seriesName,
            "Training set", 
            "Desired output/Obtained result", 
            dataset,
            PlotOrientation.VERTICAL,
            false,
            true,
            false
        );
        
        XYPlot plot = (XYPlot) chart.getPlot();
        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        renderer.setSeriesLinesVisible(0, true);
        renderer.setSeriesShapesVisible(0, false);
        renderer.setSeriesLinesVisible(1, false);
        renderer.setSeriesShapesVisible(1, true); 
        plot.setRenderer(renderer);
        
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(1000, 570));
        setContentPane(chartPanel);
        this.pack();
        RefineryUtilities.centerFrameOnScreen(this);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
    
    private XYSeries createSerie(double[][] x, double[][] y, String name) {
        final XYSeries serie = new XYSeries(name);
        for (int i = 0; i < x.length; i++) {
            for (int j = 0; j < x[i].length; j++) {
                serie.add(x[i][j], y[i][j]);
            }
        }
        
        return serie;
    }
    
    
}

